package com.sduept.nwld.mobile.rest;

import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;


import com.sduept.nwld.dataserver.manager.countanalyse.ProtectAnalysisManger;
import com.sduept.nwld.dataserver.manager.countanalyse.ProtectKpiAnalysisManger;
import com.sduept.nwld.dataserver.manager.statistics.StatisticsCommonManager;
import com.sduept.nwld.dataserver.model.countanalyse.FaultFastRemove;
import com.sduept.nwld.dataserver.model.countanalyse.ProtectCorrectAct;
import com.sduept.nwld.mobile.manager.FaultReportMobileManager;
import com.sduept.nwld.mobile.manager.MobileUtil;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 客户端保护动作指标rest
 *
 */
@RestController
@RequestMapping("/mobileProtectKpi")
public class MobileProtectKpiService {
	@Autowired
	private ProtectAnalysisManger protectAnalysisManger;
	@Autowired
	private FaultReportMobileManager faultReportMobileManager;
	//SQL注入检测类
	@Inject
	private MobileUtil mobileUtil;
	@Autowired
	private ProtectKpiAnalysisManger analysisM;
	@Autowired
	private StatisticsCommonManager commonM;

	/**
	 * 首页指标数据（一次设备故障次数、保护正确动作率、故障快速切除率）
	 */
	@GetMapping("/getKPIData")
	public String getKPIData(){
		JSONObject jso = new JSONObject();
		Calendar calendar = Calendar.getInstance();
		int currentYear = calendar.get(Calendar.YEAR);
		int currentMonth = calendar.get(Calendar.MONTH) ;
		int monthYear = currentYear;//上月所对应的年
		Integer vol=null;
		int unitType=1;
	//	Float actCorrectYear = protectAnalysisManger.getActRealityAverage(currentYear,null,vol,unitType);//全年保护正确动作率
		if(currentMonth == 0){//每年一月份
			currentMonth = 12;
			monthYear = monthYear - 1;
		}
	//	Float actCorrectMonth = protectAnalysisManger.getActRealityAverage(monthYear, currentMonth,vol,unitType);//上月保护正确动作率
		//Float quickResectYear = protectAnalysisManger.getRemovalRealityAverage(currentYear, null,vol,unitType);//全年故障快速切除率
		//Float quickResectMonth = protectAnalysisManger.getRemovalRealityAverage(monthYear, currentMonth,vol,unitType);//上月故障快速切除率
		int primFaultYear = faultReportMobileManager.primFaultStatistic(currentYear, 0);//全年一次设备故障次数
		int primFaultMonth = faultReportMobileManager.primFaultStatistic(currentYear, calendar.get(Calendar.MONTH)+1);//本月一次设备故障次数
		jso.put("actCorrectYear", "100%");
		jso.put("actCorrectMonth", "100%");
		jso.put("quickResectYear", "100%");
		jso.put("quickResectMonth","100%");
		jso.put("primFaultYear", primFaultYear);
		jso.put("primFaultMonth", primFaultMonth);

		return jso.toString();
	}

	/**
	 * 根据年月获取保护正确动作率(暂时按照调管单位统计,全部电压等级)
	 *
	 * */
	@GetMapping("/getProtectCorrectActByDate/{date}")
	public String getProtectionKpisByConditions(@PathVariable("date") String date) {
		Integer vol=null;
		int unitType=1;
		try {
			date = mobileUtil.SQLInjectCheck(date);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		String conditions = date.replace("\"", "");
		String[] str = conditions.split("-");
		int year = Integer.parseInt(str[0]);
		int month = Integer.parseInt(str[1]);
		List<ProtectCorrectAct> acts;
		if(month==0) {
			acts = analysisM.getSummaryOfCorrectActsBySubCompany(year, null, vol, unitType);
		}else {
			acts = analysisM.getSummaryOfCorrectActsBySubCompany(year, month, vol, unitType);
		}
		//List<ProtectCorrectAct> acts = protectAnalysisManger.getCorrectActsByYear(year, month);
		List<String> units = commonM.getAllUnits(unitType);//根据类型获取单位信息
		StringBuffer buff = new StringBuffer();
		buff.append("[");
		if(!acts.isEmpty()) {
			//找出没有统计数据的单位
			for (ProtectCorrectAct act : acts) {
				String subCompany = act.getSubCompany();//单位
				if(units.contains(subCompany)) {
					units.remove(subCompany);
				}
			}
			//没有统计数据的单位默认设置为100%，保持展示形状
			for(String unit : units) {
				ProtectCorrectAct pca = new ProtectCorrectAct();
				pca.setSubCompany(unit);
				pca.setPlan(100f);
				pca.setReality(100f);
				acts.add(pca);
			}

			int index=0;
			for (ProtectCorrectAct act : acts) {
				index++;
				buff.append("{\"name\":\"" + act.getSubCompany() + "\",\"plan\":\""
						+ act.getPlan() + "\",\"reality\":\"" + act.getReality() + "\"}");
				if(index!=acts.size()){
					buff.append(",");
				}
			}
		}
		buff.append("]");
		return buff.toString();
	}
	/**
	 * 根据年月获取故障快速切除率(暂时按照调管单位统计,全部电压等级)
	 *
	 * */
	@GetMapping("/getFaultFastRemoveByDate/{date}")
	public String getFaultFastRemoveByDate(@PathVariable("date") String date) {
		Integer vol=null;
		int unitType=1;
		try {
			date = mobileUtil.SQLInjectCheck(date);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		String conditions = date.replace("\"", "");
		String[] str = conditions.split("-");
		int year = Integer.parseInt(str[0]);
		int month = Integer.parseInt(str[1]);
		List<FaultFastRemove> removals;
		if(month==0) {
			removals = analysisM.getSummaryOfFaultFastRemoveBySubCompany(year, null, vol, unitType);
		}else {
			removals = analysisM.getSummaryOfFaultFastRemoveBySubCompany(year, month, vol, unitType);
		}
		List<String> units = commonM.getAllUnits(unitType);//根据类型获取单位信息
		StringBuffer buff = new StringBuffer();
		buff.append("[");
		if(!removals.isEmpty()) {
			//找出没有统计数据的单位
			for (FaultFastRemove act : removals) {
				String subCompany = act.getSubCompany();//单位
				if(units.contains(subCompany)) {
					units.remove(subCompany);
				}
			}
			//没有统计数据的单位默认设置为100%，保持展示形状
			for(String unit : units) {
				FaultFastRemove pca = new FaultFastRemove();
				pca.setSubCompany(unit);
				pca.setPlan(100f);
				pca.setReality(100f);
				removals.add(pca);
			}
			//			List<FaultFastRemove> removals = protectAnalysisManger.getFaultFastRemoveByYear(year,
			//					month);

			int index=0;
			for (FaultFastRemove removal : removals) {
				index++;
				buff.append("{\"name\":\"" + removal.getSubCompany() + "\",\"plan\":\""
						+ removal.getPlan() + "\",\"reality\":\"" + removal.getReality() + "\"}");
				if(index!=removals.size()){
					buff.append(",");
				}
			}
		}
		buff.append("]");
		return buff.toString();
	}

}